﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using WindowsGameInput;



namespace WindowsGame
{
	public delegate void SelectorEvent(Selector sender, bool plus);

	public class Selector
	{
		Button minusButton;
		Button plusButton;
		Text caption;
		Text selected;

		public event SelectorEvent SelectionChange;



		public Selector()
		{
		}



		public Selector(Texture2D plus, Texture2D plusRollOver, Texture2D minus, Texture2D minusRollOver,
						Texture2D captionIn, Texture2D selectedIn, Vector2 pos, int resolutionWidth)
		{
			float scale = resolutionWidth / 1280.0f;
			Vector2 newPos = pos;
			caption = new Text(captionIn, newPos);
			newPos.X += 0.2344f * resolutionWidth;
			plusButton = new Button(plus, plusRollOver, newPos);
			newPos.X += plus.Width * scale + 0.025f * resolutionWidth;
			selected = new Text(selectedIn, newPos);
			newPos.X += 0.15f * resolutionWidth;
			minusButton = new Button(minus, minusRollOver, newPos);

			plusButton.ButtonClick += () => SelectionChange(this, true);
			minusButton.ButtonClick += () => SelectionChange(this, false);
		}



		public Selector(Texture2D plus, Texture2D plusRollOver, Texture2D minus, Texture2D minusRollOver,
				Texture2D captionIn, string selectedIn, Color selectedColor, SpriteFont selectedSpriteFont,
				Vector2 pos, int resolutionWidth)
		{
			float scale = resolutionWidth / 1280.0f;
			Vector2 newPos = pos;
			caption = new Text(captionIn, newPos);
			newPos.X += 0.2344f * resolutionWidth;
			plusButton = new Button(plus, plusRollOver, newPos);
			newPos.X += plus.Width * scale + 0.025f * resolutionWidth;
			selected = new Text(selectedIn, selectedColor, newPos + new Vector2(0.0f, 2.0f), selectedSpriteFont);
			newPos.X += 0.15f * resolutionWidth;
			minusButton = new Button(minus, minusRollOver, newPos);

			plusButton.ButtonClick += () => SelectionChange(this, true);
			minusButton.ButtonClick += () => SelectionChange(this, false);
		}



		public void ChangeSelected(Texture2D selectedIn)
		{
			selected.ChangeCaption(selectedIn);
		}



		public void ChangeSelected(string selectedIn)
		{
			selected.ChangeCaption(selectedIn);
		}



		public void Update(GameTime gameTime)
		{
			minusButton.Update(gameTime);
			plusButton.Update(gameTime);
			caption.Update(gameTime);
			selected.Update(gameTime);
		}



		public void Draw(SpriteBatch spriteBatch, GameTime gameTime, float scale)
		{
			minusButton.Draw(spriteBatch, gameTime, scale);
			plusButton.Draw(spriteBatch, gameTime, scale);
			caption.Draw(spriteBatch, gameTime, scale);
			selected.Draw(spriteBatch, gameTime, scale);
		}
	}
}
